package com.wsm.common.listenner;

import com.wsm.common.annotation.JobTask;
import com.wsm.common.domain.TaskClassDO;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CustomAnnotationDemo
 * @Description: 自定义类注解、方法注解demo
 * @Author: RuiXin Yu
 * @Date: 2019/3/21 10:35
 */
@Component
public class TaskLocationListener implements ApplicationListener<ContextRefreshedEvent> {

    public static List<TaskClassDO> invokerList = new ArrayList<>();

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 根容器为Spring容器
        if (event.getApplicationContext().getParent() == null) {
            Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(JobTask.class);
            for (Object bean : beans.values()) {
                JobTask annotation = bean.getClass().getAnnotation(JobTask.class);
                invokerList.add(new TaskClassDO(bean.getClass().getName(), annotation.value()));
            }
        }
    }
}
